import { ref } from 'vue'
import { checkQuote, getOrderFeeDetail, OrderFeeDetailProjectInfo } from '@/api/order'

/**
 * 审核报价
 */
export const useCheckQuote = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref({
    checkResult: true,
    remark: '',
  })

  /**
   * 重置审核表单
   */
  const resetCheckForm = () => {
    checkForm.value = {
      checkResult: true,
      remark: '',
    }
  }

  /**
   * 审核方法
   * @param {string} orderId - 工单id
   */
  const check = async (orderId: string) => {
    checkLoading.value = true
    const res = await checkQuote(checkForm.value, orderId)
    checkLoading.value = false
    return res.data
  }

  return {
    checkForm,
    checkLoading,
    check,
    resetCheckForm,
  }
}

/**
 * 获取服务费用详情
 */
export const useGetFeeDetail = () => {
  // 获取详情加载
  const getFeeDetailLoading = ref(false)

  // 费用详情
  const feeDetail = ref<OrderFeeDetail>({
    id: '',
    workNo: '',
    feeNo: '',
    status: 1,
    applyRemark: '',
    applyEmpType: 1,
    applyEmpId: '',
    applyEmp: '',
    receive: true,
    reviewEmp: '',
    reviewTime: null,
    reviewRemark: '',
    createTime: '',
    updateTime: '',
    totalPrice: 0,
    settlementType: '',
    surePrice: true,
  })

  /**
   * 获取服务费用详情
   * @param {string} id - 服务费用id
   */
  const getFeeDetail = async (id: string) => {
    getFeeDetailLoading.value = true
    const res = await getOrderFeeDetail(id)
    feeDetail.value = res.data.data.row
    getFeeDetailLoading.value = false
  }

  return {
    feeDetail,
    getFeeDetailLoading,
    getFeeDetail,
  }
}

/**
 * 获取服务项目列表
 */
export const useGetFeeListByFeeId = () => {
  // 获取服务项目列表加载
  const getFeeListLoading = ref(false)

  // 费用项目列表
  const feeList = ref<OrderFeeProjectByFeeId>({
    delFeeDetailIds: [],
    workId: '',
    remark: '',
    id: '',
    feeDetails: [],
  })

  /**
   * 获取费用项目
   * @param {stirng} feeId - 费用id
   */
  const getFeeList = async (feeId: string) => {
    getFeeListLoading.value = true
    const res = await OrderFeeDetailProjectInfo(feeId)
    feeList.value = res.data.data.row
    getFeeListLoading.value = false
  }

  return {
    feeList,
    getFeeListLoading,
    getFeeList,
  }
}
